﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.SalePurchase.Sale;
using PJ.Framework.DynamicsAX;
using Microsoft.Dynamics.BusinessConnectorNet;
using PJ.Business.SalePurchase;

namespace PJ.Operation.SalePurchase.Sale
{

    public class AxCreateSaleInvoice : AxCreateInvoice, ICreateSaleInvoice
    {
        #region Interface implementation


        public override ICreateInvoiceResult Execute(string partnerId, string reference, List<string> inventoryItemIds, List<double> quantities, List<string> units)
        {
            using (AxaptaContainer itemIdContainer = DynamicsAXBroker.CreateContainer())
            {
                itemIdContainer.AddRange(inventoryItemIds);
                using (AxaptaContainer quantityContainer = DynamicsAXBroker.CreateContainer())
                {
                    quantityContainer.AddRange(quantities);
                    using (AxaptaContainer unitContainer = DynamicsAXBroker.CreateContainer())
                    {
                        unitContainer.AddRange(units);
                        // string test = (string)DynamicsAX.DynamicsAXBroker.StaticMethod("PJKSaleInvoiceAXProvider", "testTempName");
                        int[] pdfi = (int[])DynamicsAXBroker.StaticMethod("PJKSaleInvoiceAXProvider", "PostInvoice",
                                    new object[] {partnerId, reference, itemIdContainer, quantityContainer, unitContainer });
                        byte[] pdf = new byte[pdfi.Length];
                        for (int i = 0; i < pdfi.Length; i++)
                            pdf[i] = (byte)pdfi[i];
                        return new CreateInvoiceResult() { PdfDocument = pdf };
                    }
                }
            }
        }
        #endregion

    }
}
